using System;
using DesignerHelper.Main;
using SpreadsheetGear;

namespace DesignerHelper.Addons.TemplateConvertor
{
    public class CtpxConvertor : FileConvertor
    {
        protected sealed override IWorkbook WorkOn { get; }
        //protected sealed override LockBookWorker LbwTrigger { get; set; }
        protected sealed override IWorkbook NewCtpx { get; }
        public CtpxConvertor(IWorkbook ctpxO, IWorkbook ctpxNew)
        {
            WorkOn = ctpxO;
            NewCtpx = ctpxNew;
            //LbwTrigger = LockBookWorker.GetWorker(WorkOn);
        }

        public override void BeginConvert()
        {
            IWorksheet sheetStockLibrary = NewCtpx.Worksheets[0];
            IWorksheet solidStockLibrary = NewCtpx.Worksheets[1];
            IWorksheet buyOutLibrary = NewCtpx.Worksheets[2];
            CopyTo(WorkOn.Worksheets["SheetStockLibrary"], sheetStockLibrary.Range, true);
            CopyTo(WorkOn.Worksheets["SolidStockLibrary"], solidStockLibrary.Range, true);
            CopyTo(WorkOn.Worksheets["BuyOutLibrary"], buyOutLibrary.Range, true);

            NewCtpx.Names.Add("MFSH", $"{'='}\'{sheetStockLibrary.Name}\'{'!'}{GetRangeAddress(WorkOn.Names["MFSH"].RefersToRange)}");
            NewCtpx.Names.Add("MFSO", $"{'='}\'{solidStockLibrary.Name}\'{'!'}{GetRangeAddress(WorkOn.Names["MFSO"].RefersToRange)}");
            NewCtpx.Names.Add("MFBO", $"{'='}\'{buyOutLibrary.Name}\'{'!'}{GetRangeAddress(WorkOn.Names["MFBO"].RefersToRange)}");
        }
        protected sealed override void CopyTo(IWorksheet source, IRange target, bool renameTargetWorksheet)
        {
            base.CopyTo(source, target, renameTargetWorksheet);
            AutoFit(target["$A:$A"].Columns, target["$E:$E"].Columns);
        }
        protected sealed override string GetRangeAddress(IRange range)
        {
            return range.GetAddress(true, true, ReferenceStyle.A1, false, null);
        }
    }
}